﻿/*******************************************************************************
 * Copyright @ 2017 YunSpace.Framework 版权所有
 * Author: GaoTang
 * Description: YunSpace 快速开发平台
 * Website：http://www.yunspace.com.cn
*********************************************************************************/
#region USING SYSTEM
using YunSpace.Core;
using YunSpace.Domain.Entity.SiteLibraryManage;
using YunSpace.Domain.IRepository.SiteLibraryManage;
using YunSpace.Repository.SiteLibrary;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Data.Common;
using MySql.Data.MySqlClient;
#endregion

namespace YunSpace.Application.SiteLibraryManage
{
    public class PhotosApp
    {
        private IPhotosRepository service = new PhotosRepository();

        public List<PhotosEntity> GetList(Pagination pagination, string queryJson)
        {
            var expression = ExtLinq.True<PhotosEntity>();
            var queryParam = queryJson.ToJObject();
            if (!queryParam["photoable_id"].IsEmpty() && !queryParam["photoable_type"].IsEmpty())
            {

                string photoable_id = queryParam["photoable_id"].ToString();

                string photoable_type = queryParam["photoable_type"].ToString();

                expression = expression.And(t => t.photoable_id == photoable_id && t.photoable_type == photoable_type);

            }

            // if (!queryParam["keyword"].IsEmpty())
            // {
            //     string keyword = queryParam["keyword"].ToString();
            //     expression = expression.And(t => t.F_Account.Contains(keyword));
            // }
            // if (!queryParam["timeType"].IsEmpty())
            // {
            //     string timeType = queryParam["timeType"].ToString();
            //     DateTime startTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate();
            //     DateTime endTime = DateTime.Now.ToString("yyyy-MM-dd").ToDate().AddDays(1);
            //     switch (timeType)
            //     {
            //         case "1":
            //             break;
            //         case "2":
            //             startTime = DateTime.Now.AddDays(-7);
            //             break;
            //         case "3":
            //             startTime = DateTime.Now.AddMonths(-1);
            //             break;
            //         case "4":
            //             startTime = DateTime.Now.AddMonths(-3);
            //             break;
            //         default:
            //             break;
            //     }
            //     expression = expression.And(t => t.F_Date >= startTime && t.F_Date <= endTime);
            // }
            return service.FindList(expression, pagination);
        }
        public List<PhotosEntity> GetList(string queryJson)
        {
            var expression = ExtLinq.True<PhotosEntity>();
            
            var queryParam = queryJson.ToJObject();
            if (!queryParam["photoable_id"].IsEmpty() && !queryParam["photoable_type"].IsEmpty())
            {

                string photoable_id = queryParam["photoable_id"].ToString();

                string photoable_type = queryParam["photoable_type"].ToString();

                expression = expression.And(t => t.photoable_id == photoable_id && t.photoable_type == photoable_type );

            }
            expression = expression.And(t => t.F_DeleteMark != true);

            return service.IQueryable(expression).OrderByDescending(t => t.is_cover).ThenByDescending(t => t.F_CreatorTime) .ToList();
        }
        public PhotosEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void DeleteFormbyphotoid(string photoable_id)
        {
            service.Delete(t => t.photoable_id == photoable_id);
        }
        public void SubmitForm(PhotosEntity photosEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                photosEntity.Modify(keyValue);
                service.Update(photosEntity);
            }
            else
            {
                photosEntity.Create();
                // photosEntity.F_Category = 2;
                service.Insert(photosEntity);
            }
        }
        /// <summary>
        /// 批量修改图片信息
        /// </summary>
        /// <param name="pList"></param>
        public void updatePhotoList(List<PhotosEntity> pList,string photoable_id) {
            for (int i = 0; i < pList.Count; i++)
            {
                pList[i].photoable_id = photoable_id;
                if (pList[i].F_Id.Length > 4)
                {
                    pList[i].Modify(pList[i].F_Id);
                    service.Update(pList[i]);
                }
                else if (pList[i].F_DeleteMark == true) {
                    pList[i].Remove();
                    service.Update(pList[i]);
                }
                else
                {
                    pList[i].Create();
                    pList[i].F_DeleteMark = false;
                    service.Insert(pList[i]);
                }
            }
        }

        public List<PhotosEntity> GetListByActivityId(string id,string type) {           
            var expression = ExtLinq.True<PhotosEntity>();
            expression = expression.And(t=> t.photoable_id==id && t.photoable_type==type);
            return service.IQueryable(expression).OrderBy(t => t.F_Id).ToList();
        }







    }
}

